<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 吃草的鱼 <783188184@qq.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace Home\Controller;
use Admin\Api\OrdersPaycodeApi;
use Admin\Api\WeixinLogApi;
use Think\Controller;
use Shop\Api\CategoryApi;
use Shop\Api\ProductApi;
use Shop\Api\ProductSkuApi;
use Shop\Api\ProductImageApi;
use Weixin\Api\WxuserApi;
use Admin\Api\UserPictureApi;
use Shop\Api\AddressApi;
use Shop\Api\OrdersApi;
use Shop\Api\OrdersItemApi;
use Shop\Api\OrdersContactInfoApi;
use Weixin\Api\WxaccountApi;
use Common\Api\WeixinApi;
use Home\Controller\GroupController;
class TestPayController extends Controller
{
    public function __construct()
    {
        parent::__construct();
        // $this->getConfig();
        $result = apiCall(WxaccountApi::GET_INFO, array(array('id' => 5)));
        if ($result['status'] && is_array($result['info'])) {
            $this->wxaccount = $result['info'];
        } else {
            exit("公众号信息获取失败，请重试！");
        }
    }
    /*
     * 异步结果通知地址
     * */
    public function ajaxinformyuan(){
        ignore_user_abort(TRUE);
        //如果客户端断开连接，不会引起脚本abort
        set_time_limit(0);
        $xml = $GLOBALS['HTTP_RAW_POST_DATA'];
        $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        if(empty($array_data['out_trade_no'])){
            echo "<xml>
                    <return_code><![CDATA[FAIL]]></return_code>
                    <return_msg><![CDATA[数据接收失败]]></return_msg>
                 </xml>";
        }else{
            //开启事务
            /*$modal = M();
            $modal->startTrans();*/
            $flag = false;
            $map['order_code'] = $array_data['out_trade_no'];//返回订单号
            $save['pay_status'] = 1;
            $save['order_status'] = 3;
            $save['pay_code'] = $array_data['transaction_id'];//微信支付订单号
            $orderres = apiCall(OrdersApi::SAVE,array($map,$save));
            if($orderres['status']){
                //查出用户ID
                $wxmap['openid'] = $array_data['openid'];;
                $orderres = apiCall(WxuserApi::GET_INFO,array($wxmap));
                if($orderres['status']){
                    $add['createtime'] = time();
                    $add['uid'] = $orderres['info']['id'];
                    $add['pay_type'] = 2;
                    $add['pay_money'] = $array_data['total_fee'];
                    $add['pay_code'] = $array_data['transaction_id'];//微信支付订单号
                    $orderadd = apiCall(OrdersPaycodeApi::ADD,array($add));
                    if(!$orderadd['status']){
                        $flag = true;
                    }
                }else{
                    $flag = true;
                }

            }else{
                $flag = true;
            }
        }

        if($flag){
            /*$modal->rollback();*/
            echo "<xml>
                    <return_code><![CDATA[FAIL]]></return_code>
                    <return_msg><![CDATA[数据接收失败]]></return_msg>
                 </xml>";
        }else{

                $order_code = $array_data['out_trade_no'];
                $groupid=session('groupid');
                $group=new GroupController();
                $group->yuaningroup($order_code,$groupid);

        }
    }



    public function ajaxinformzhang(){
        ignore_user_abort(TRUE);
        //如果客户端断开连接，不会引起脚本abort
        set_time_limit(0);
        $xml = $GLOBALS['HTTP_RAW_POST_DATA'];
        $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        if(empty($array_data['out_trade_no'])){
            echo "<xml>
                    <return_code><![CDATA[FAIL]]></return_code>
                    <return_msg><![CDATA[数据接收失败]]></return_msg>
                 </xml>";
        }else{
            //开启事务
            /*$modal = M();
            $modal->startTrans();*/
            $flag = false;
            $map['order_code'] = $array_data['out_trade_no'];//返回订单号
            $save['pay_status'] = 1;
            $save['order_status'] = 3;
            $save['pay_code'] = $array_data['transaction_id'];//微信支付订单号
            $orderres = apiCall(OrdersApi::SAVE,array($map,$save));
            if($orderres['status']){
                //查出用户ID
                $wxmap['openid'] = $array_data['openid'];;
                $orderres = apiCall(WxuserApi::GET_INFO,array($wxmap));
                if($orderres['status']){
                    $add['createtime'] = time();
                    $add['uid'] = $orderres['info']['id'];
                    $add['pay_type'] = 2;
                    $add['pay_money'] = $array_data['total_fee'];
                    $add['pay_code'] = $array_data['transaction_id'];//微信支付订单号
                    $orderadd = apiCall(OrdersPaycodeApi::ADD,array($add));
                    if(!$orderadd['status']){
                        $flag = true;
                    }
                }else{
                    $flag = true;
                }

            }else{
                $flag = true;
            }
        }

        if($flag){
            /*$modal->rollback();*/
            echo "<xml>
                    <return_code><![CDATA[FAIL]]></return_code>
                    <return_msg><![CDATA[数据接收失败]]></return_msg>
                 </xml>";
        }else{


                $order_code = $array_data['out_trade_no'];
                $group=new GroupController();
                $group->beinggroup($order_code);



        }
    }

}
